﻿package org.openChart.sprite 
{
	import flash.display.FrameLabel;
	import flash.display.Sprite;
	import flash.events.Event;
	import org.openChart.display.chart.DisplayBarElement;
	import org.openChart.display.chart.DisplayPieElement;
	import org.openChart.display.DisplayBarChart;
	import org.openChart.display.DisplayChart;
	import org.openChart.display.DisplayColorLabelGroup;
	import org.openChart.display.DisplayPieChart;
	import org.openChart.display.DisplayScreen;
	import org.openChart.display.DisplayStick;
	import org.openChart.display.DisplayTextLabel;
	import org.openChart.meta.BarChart;
	import org.openChart.meta.BaseChart;
	import org.openChart.meta.manager.CSSLoader;
	import org.openChart.meta.manager.XmlDataLoader;
	import org.openChart.meta.manager.XmlDefinationLoader;
	import org.openChart.meta.PieChart;
	import org.openChart.meta.YAxis;
	import org.openChart.meta.manager.LoaderEvent;
	import org.openChart.meta.ChartType;
	
	/**
	 * The main frame is the only entrance of chart.
	 * 
	 * @author Chongyuan
	 */
	public class MainFrame extends ContainerSprite implements CommandHandler
	{
		private var xmlDefinationLoader:XmlDefinationLoader;
		private var xmlDataLoader:XmlDataLoader;
		private var cssLoader:CSSLoader;
		private var screen:DisplayScreen;
		
		public function MainFrame() 
		{
			
			xmlDefinationLoader = new XmlDefinationLoader();
			xmlDefinationLoader.addEventListener(LoaderEvent.LOADED, definationLoaded);
			xmlDefinationLoader.addEventListener(LoaderEvent.FAILED, definationFailed);
			xmlDefinationLoader.addEventListener(LoaderEvent.EXCEPTION, definationException);
			
			CSSLoader.newInstance();
			cssLoader = CSSLoader.instance();
			cssLoader.addEventListener(LoaderEvent.LOADED, cssLoaded);
			cssLoader.addEventListener(LoaderEvent.FAILED, cssFailed);
			cssLoader.addEventListener(LoaderEvent.EXCEPTION, cssException);
			
			super(null, null);
			
		}
		
		public override function processCommand( command:uint, traget:BaseSprite):void
		{
			
		}
		
		public function dataLoaded():void
		{
			var chart:BaseChart = xmlDefinationLoader.chart;
			//trace(chart);
			var chartType:String = chart.chartType;
			if (chartType == ChartType.DRUSY_BAR_CHART_2D
				|| chartType == ChartType.PERCENT_BAR_CHART_2D
				|| chartType == ChartType.CUMULATE_BAR_CHART_2D
				|| chartType == ChartType.CUMULATE_LINE_CHART
				|| chartType == ChartType.PERCENT_LINE_CHART
				|| chartType == ChartType.DRUSY_LINE_CHART
				|| chartType == ChartType.DRUSY_BAR_CHART_3D_CUBE
				|| chartType == ChartType.CUMULATE_BAR_CHART_3D_CUBE
				|| chartType == ChartType.PERCENT_BAR_CHART_3D_CUBE
				|| chartType == ChartType.CUMULATE_BAR_CHART_3D_CYLINDER
				|| chartType == ChartType.DRUSY_BAR_CHART_3D_CYLINDER
				|| chartType == ChartType.PERCENT_BAR_CHART_3D_CYLINDER
				|| chartType == ChartType.DRUSY_BAR_CHART_3D_CONE
				|| chartType == ChartType.CUMULATE_BAR_CHART_3D_CONE
				|| chartType == ChartType.PERCENT_BAR_CHART_3D_CONE
				|| chartType == ChartType.DRUSY_BAR_CHART_3D_PYRAMID
				|| chartType == ChartType.CUMULATE_BAR_CHART_3D_PYRAMID
				|| chartType == ChartType.PERCENT_BAR_CHART_3D_PYRAMID)
			{
				initBarChart(chart);
			}
			else if (chartType == ChartType.PIE_CHART_2D
				|| chartType == ChartType.PIE_CHART_3D
				|| chartType == ChartType.DISCONNECTED_PIE_CHART_2D
				|| chartType == ChartType.DISCONNECTED_PIE_CHART_3D)
			{
				initPieChart(chart);
			}
		}
		
		private function initBarChart( chart:BaseChart ):void
		{
			var barChart:BarChart = chart as BarChart;
			screen = new DisplayScreen(0, stage.stageHeight, 0, stage.stageWidth, barChart);
			screen.initialize();
			screen.resize();
			fillBackGround(screen.bgcolor);
			
			if ( screen.title){
				var title:TextLabelSprite = new TextLabelSprite(screen.title, this);
				this.addChild(title);
			}
			
			var displayChart:DisplayBarChart = screen.topChart as DisplayBarChart;
			
			var xAxisSprite:XAxisSprite = new XAxisSprite(displayChart.xDisplayAxis, this);
			this.addChild(xAxisSprite);
			
			var yAxisSprite:YAxisSprite = new YAxisSprite(displayChart.yDisplayAxis, this);
			this.addChild(yAxisSprite);
			
			var groupSetSprite:GroupSetSprite = new GroupSetSprite(displayChart.groupSet, this);
			this.addChild(groupSetSprite);
			
			var displayColorLabels:DisplayColorLabelGroup = screen.labels;
			var colorGroup:ColorLabelGroup = new ColorLabelGroup(displayColorLabels, this);
			addChild(colorGroup);
			this.repaint();
		}
		
		
		private function fillBackGround( color:uint ):void
		{
			//trace(this + " fill background with color:" + color);
			graphics.beginFill(color, 1.0);
			graphics.moveTo(0, 0);
			graphics.lineTo(0, stage.stageHeight);
			graphics.lineTo(stage.stageWidth, stage.stageHeight);
			graphics.lineTo(stage.stageWidth, 0);
			graphics.lineTo(0, 0);
			graphics.endFill();
		}
		
		private function initPieChart( chart:BaseChart ):void
		{
			var pieChart:PieChart = chart as PieChart;
			
			var screen:DisplayScreen = new DisplayScreen(0, stage.stageHeight, 0, stage.stageWidth, pieChart);
			//screen.addChart(pieChart);
			screen.initialize();
			screen.resize();
			fillBackGround( screen.bgcolor);
			//trace(pieChart.groupSet);
			
			var displayChart:DisplayPieChart = screen.topChart as DisplayPieChart;
			
			var groupSetSprite:GroupSetSprite = new GroupSetSprite(displayChart.groupSet, this);
			this.addChild(groupSetSprite);
			
			var displayColorLabels:DisplayColorLabelGroup = screen.labels;
			var colorGroup:ColorLabelGroup = new ColorLabelGroup(displayColorLabels, this);
			addChild(colorGroup);
			
			if ( screen.title){
				var title:TextLabelSprite = new TextLabelSprite(screen.title, this);
				this.addChild(title);
			}
			
		}
		
		private function definationLoaded( event:LoaderEvent):void
		{
			dataLoaded();
		}
		
		private function definationFailed( event:LoaderEvent ):void
		{
			
		}
		
		private function definationException( event:LoaderEvent ):void
		{
			
		}
		
		private function cssLoaded( event:LoaderEvent):void
		{
			xmlDefinationLoader.loadXml();
		}
		
		private function cssFailed( event:LoaderEvent ):void
		{
			
		}
		
		private function cssException( event:LoaderEvent ):void
		{
			
		}
	}
	
}